const router = require('koa-router')()

router.get('/', async (ctx, next) => {
  ctx.cookies.set('pvid', Math.random()) // 设置cookie
  await ctx.render('index', { // 返回页面
    title: 'Hello Koa 2!'
  })
})

router.get('/string', async (ctx, next) => {
  ctx.body = 'koa2 string' // 返回接口
})

router.get('/json', async (ctx, next) => {
  ctx.body = {
    title: 'koa2 json',
    cookie: ctx.cookies.get('pvid')
  }
})

router.get('/getAsync', async (ctx, next) => {
  const name = await new Promise((resolve, reject) =>{
      setTimeout(() => {
        resolve('123')
      }, 1000);
  })
  const age = await new Promise((resolve, reject) =>{
    setTimeout(() => {
      resolve(18)
    }, 1000);
})
const sex = await '男'
  ctx.body = {
    name,
    age,
    sex
  }
})

module.exports = router
